#include <iostream>
#include "cpp-httplib/httplib.h"
#include "searcher.hpp"


const std::string root_path = "./wwwroot";
const static std::string input = "./data/raw_html/raw.txt";

int main(){
    ns_searcher::Searcher search;
    search.InitSearcher(input);
    
    httplib::Server svr;
    svr.set_base_dir(root_path.c_str());
    svr.Get("/s", [&search](const httplib::Request& req, httplib::Response& resp)
    {
        if(!req.has_param("word")){
            resp.set_content("搜索需要提供关键字", "text/plain;charset=utf-8");
            return;
        }
        std::string word = req.get_param_value("word");

        // std::cout << "搜索:" << word << std::endl;
        LOG(INFO,"用户搜索" + word);
        
        std::string json_str;
        search.Search(word, &json_str);
        resp.set_content(json_str, "application/json");
    });
    
    LOG(INFO, "服务器启动成功");
    svr.listen("0.0.0.0", 8081);
    return 0;
}
